#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include <fstream>

using namespace std;
const string defaultfile = "./dict.txt";
const string sep = ": "; 

class Dict
{
    void Load()
    {
        fstream in(_filename);
        if(!in.is_open())
        {
            cerr << "file open error" << endl;
            return;
        }

        string line;
        while(getline(in, line))
        {
            if(line.empty()) continue;
            auto pos = line.find(sep);
            if(pos == string::npos) continue;
            string word = line.substr(0, pos);
            if(word.empty()) continue;
            string han = line.substr(pos+sep.size());
            if(han.empty()) continue;

            _dict.insert({word, han});
        }
        in.close();
        cout << "file open success" << endl;
    }
public:
    Dict(const string &name = defaultfile):_filename(name)
    {
        Load();
    }

    string Translate(const string &word)
    {
        auto pos = _dict.find(word);
        if(pos == _dict.end())
        {
            return "未找到";
        }
        return pos->second;
    }

    ~Dict()
    {}

private:
    unordered_map<string, string> _dict;
    string _filename;
};